const app = getApp();
const url = require('../../http/url.js');

let that;
Page({
  data: {
    codeMsg: '获取验证码',
    time: 60,
    phone: '',
    code: '',
    codeFlag: true,
    companyOpenId: 0,
    companyId: 0,
    userId: 0,
    staffInfoShowFlag: 1,
    text:'请输入企业邮箱/手机号',
    title: 'Printing',
  },

  onLoad: function(options) {
    that = this;
    let code = wx.getStorageSync('code');
    if (code) {
      let params = {
        password: code,
      }
      app.post(url.login, params, function (res) {
        if (res.code == 200) {
          app.data.user = res.data;
          wx.setStorageSync('code', that.data.code);
          wx.setStorageSync('isLogin', true)
          wx.navigateTo({
            url: '../printList/printList',
          })
          return;
        }
      })
    }
  },

  codeInput: function(e) {
    that.setData({
      code: e.detail.value,
    })
  },

  confirm: function () {
    if (!that.data.code) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let params = {
      password: that.data.code,
    }
    app.post(url.login, params, function (res) {
      if (res.code == 200) {
        app.data.user = res.data;
        wx.setStorageSync('code', that.data.code);
        wx.setStorageSync('isLogin', true)
        wx.redirectTo({
          url: '../printList/printList',
        })
        return;
      }
      let msg = '服务君走神了哦~~';
      if (res.code != 500) {
        msg = res.msg;
      }
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      });
    })
  },
})